from goods.models import GoodsChannel


def get_categories():
    # 查询所有的频道
    channel_list = GoodsChannel.objects.order_by('group_id').order_by('sequence')
    categories = {}
    # 遍历频道，添加分类信息
    for channel in channel_list:
        # 判断频道是否存在
        if channel.group_id not in categories:
            categories[channel.group_id] = {
                'channels': [],
                'sub_cats': []
            }
            #         添加一级分类
        categories[channel.group_id]['channels'].append({'url': channel.url, 'name': channel.category.name})
        #         添加二级分类
        for sub2 in channel.category.subs.all():
            # 添加三级分类
            sub2.sub_cats = sub2.subs.all()

            categories[channel.group_id]['sub_cats'].append(sub2)
    return categories
